<?php

/**
 * Class handles sidebars articles.
 *
 * @author Honza
 */
class SidebarArticles {

  /**
   * 
   * @param boolean $news do you want to see news?
   * @param boolean $recentBooks do you want to see recentBooks?
   * @param boolean $subscribe do you want to see subscribe?
   */
  public static function addSitebarArticles($news, $recentBooks, $subscribe) {
    $pageObject = Page::getInstance();
    if ($news) {
      $pageObject->addToSidebar(SidebarArticles::getNews());
    }
    
    if ($recentBooks) {
      $pageObject->addToSidebar(SidebarArticles::getMostRecentBooks());
    }
    
    if ($subscribe) {
      $pageObject->addToSidebar(SidebarArticles::getSubscribe());
    }
  }
  
  /**
   * @return text news article
   */
  private static function getNews() {
    $sql = 'select id_news, title, text, DATE_FORMAT(date, \'%Y-%m-%d\') as date from ' . TBL_NEWS . ' order by id_news desc limit 1';
    $news = DB::sql($sql);
    $title = $news[0]['title'];
    $text = substr($news[0]['text'], 0, 100) . '... ';
    $date = $news[0]['date'];
    $idNews = $news[0]['id_news'];

    $header = "<h2>News</h2><h4>$title</h4><h5>$date</h5>";
    $body = $text;
    $footer = "<a href=\"index.php?where=news&ida=$idNews\">more ...</a>";

    return self::getArticle($header, $body, $footer);
  }

  /**
   * @return text most recent books article
   */
  private static function getMostRecentBooks() {
    $header = "<h2>Most recent books</h2>";


    $body .= "<ul>";

    //last 4 books
    $sql = 'select * from ' . TBL_BOOKS . ' order by id_book desc limit 4';
    $books = DB::sql($sql);
    foreach ($books as $book) {
      $sql = '';
      $sql .= 'select concat(upper(substr(name, 1, 1)), \'. \', surname) as author from ' . TBL_AUTHORS_BOOKS;
      $sql .= ' join ' . TBL_AUTHORS . ' using (id_author)';
      $sql .= 'where id_book = ' . $book['id_book'];
      $sql .= ' limit 1';
      $autor = DB::sql($sql);
      $body .= '<li><a href="index.php?where=detail&ida=' . $book['id_book'] . '">' . $autor[0]['author'] . ' - ' . $book['title'] . '</a></li>';
    }
    $body .= "</ul>";

    $footer = '';

    return self::getArticle($header, $body, $footer);
  }

  /**
   * @return text subscribe article
   */
  private static function getSubscribe() {
    $subscribeMessage = "";
    $isEmailValid = false;
    if (isset($_POST['subscribe'])) {
      $email = $_POST['email'];
      if (filter_var("$email", FILTER_VALIDATE_EMAIL)) {
        // email IS valid
        $subscribeMessage = "You are a subscriber.";
        $isEmailValid = true;
        
      } else {
        // email IS NOT valid
        $isEmailValid = false;
        $subscribeMessage = "Email is not valid.<br />";
      }
    }

    //compose article
    $header = "<h2>Subscribing</h2>";

    $body .= "<p>If you wish to subscribe, fill your e-mail and press the button 'Subscribe'</p>";

    if (isset($subscribeMessage) && $subscribeMessage !== "") {
      if ($isEmailValid) {
        $body .= "<p>" . $subscribeMessage . "</p>";
      } else {
        $body .= "<p class=\"error\">" . $subscribeMessage . "</p>";
      }
    }

    $f = new Form();
    $f->startForm("self", "post", null, array("id" => "subscribe"));
    $f->addInput("text", "email", "Your e-mail address", array("class" => "send_news"));
    $f->addInput("submit", "subscribe", "Subscribe", array("class" => "subscribe"));
    $body .= $f->endForm()->getForm();

    $footer = '';

    return self::getArticle($header, $body, $footer);
  }

  /**
   * 
   * @param type $header header of the article
   * @param type $body body of the article
   * @param type $footer footer of the article
   * @return string
   */
  private static function getArticle($header, $body, $footer) {
    $article .= '<img class="paperclip" src="images/paperclip.png" alt="paperclip" />';
    $article .= '<article class="sidebar">';

    $article .= '<header>' . $header . '</header>';
    $article .= '<div>' . $body . '</div>';
    if (!empty($footer)) {
      $article .= '<footer>' . $footer . '</footer>';
    }
    $article .= '</article>';
    return $article;
  }

}

?>
